﻿@inject IHttpService _httpService
@inject ILocalStorageService _localStorage

@inject IFileUploadService _fileUploadService
@inject NavigationManager NavigationManager
@inject IJSRuntime JS
@inject IDataCacheService _dataCatcheService

@inject IPopupService PopupService

@implements IDisposable


@if (string.IsNullOrWhiteSpace(Model.Name) == false)
{
    <CnGalWebSite.Shared.MasaComponent.Shared.Cards.TitleTip Title="@Model.Name" Description="@Model.BriefIntroduction" Image="@Model.MainPicture"/>
}
else
{
    <CnGalWebSite.Shared.MasaComponent.Shared.Cards.TitleTip />
}
@if (isNotFound == true)
{
    <NotFoundCard>
        <h1 class="text-center title-large">找不到这个标签哦......</h1>
        <div class="text-center">可以在这里<a href="/tags/createtag">创建标签</a>，也可以查看<a href="/articles/index/254">《标签编辑规范》</a></div>
    </NotFoundCard>
}
else
{
<div>
    @if (_dataCatcheService.TagIndexPageCatche.Check(Id) || isReady)
    {
        <div>

            @if (Model.IsHidden == true)
            {
                <CnGalWebSite.Shared.MasaComponent.Shared.Components.MasaAlert Type="AlertTypes.Warning" Class="mb-3">
                    当前标签处于隐藏状态，如果你看到了这个提示，请立即联系管理员
                </CnGalWebSite.Shared.MasaComponent.Shared.Components.MasaAlert>
            }
            <TagHeadNavTip TagList="Model.Taglevels" />
            <div class="mb-4">
                <TagMainInforCard Model="Model" OnRevoke="OnRevoke" />
            </div>

            @if (Model.ChildrenTags.Count != 0 || Model.IsEdit)
            {
                <div class="mb-3">
                    <TagListChildTagsViewTip Model="Model" OnClickRevoke="OnRevoke" />
                </div>
            }
            @if (Model.ChildrenEntries.Count != 0 || Model.IsEdit)
            {
                <div class="">
                    <TagListChildEntriesViewTip Model="Model" OnClickRevoke="OnRevoke" />
                </div>
            }

        </div>

    }
    else
    {
        <CnGalWebSite.Components.Progressings.ProgressingCard Page />

    }
</div>
}
<CnGalWebSite.Shared.MasaComponent.Shared.Components.TrackEvent DataType="TrackEventDataType.Tag" EventType="TrackEventType.View" ObjectId="@Model.Id.ToString()"
                                                                ObjectName="@Model.Name" />
@if (isFirstLoad == false)
{
    <CnGalWebSite.Shared.MasaComponent.Shared.Cards.NavLoadingCard IsLoading="isLoading" />
}

@code {
    [Parameter]
    public string Id { get; set; }

    bool isReady = false;
    bool isLoading = false;
    bool isFirstLoad = true;
    bool isNotFound = false;

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    public TagIndexViewModel Model { get; set; } = new TagIndexViewModel();
    string LastLookName = "";
    [CascadingParameter]
    private Task<AuthenticationState> authenticationStateTask { get; set; }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);
        if (firstRender)
        {
            NavigationManager.LocationChanged -= LocationChanged;
            NavigationManager.LocationChanged += LocationChanged;
            isFirstLoad = false;
        }
    }

    protected override async Task OnInitializedAsync()
    {
        await Refresh();
    }

    public async Task Refresh()
    {
        try
        {
            //isReady = false;
            StateHasChanged();
            //获取标签信息
            Model = await _dataCatcheService.TagIndexPageCatche.GetCache(Id);
            //判断标签名称是否为空 为空则跳转到主页
            if (string.IsNullOrWhiteSpace(Model.Name))
            {
                NavigationManager.NavigateTo("/");
            }

            isReady = true;
            // StateHasChanged();
        }
        catch (Exception ex)
        {
            isNotFound = true;
            await ErrorHandler.ProcessError(ex, "获取标签失败");
        }

    }

    private void OnCreate()
    {
        NavigationManager.NavigateTo("/tags/createtag");
    }


    private async Task OnRevoke(Operation operation)
    {
        var ret = await PopupService.ConfirmAsync("确认撤销编辑", "撤销编辑后会删除数据，无法恢复，请谨慎操作");
        if (ret == false)
        {
            return;
        }
        //上传
        try
        {
            var obj = await _httpService.PostAsync<RevokeExamineModel, Result>("api/tags/RevokeExamine", new RevokeExamineModel { Id = Model.Id, ExamineType = operation });
            //判断结果
            if (obj.Successful == false)
            {
                await PopupService.ToastErrorAsync("撤销审核请求失败", obj.Error);
            }
            else
            {
                await PopupService.ToastSuccessAsync("撤销审核请求成功", "撤销审核请求成功");
                //修改各部分编辑状态
                switch (operation)
                {
                    case Operation.EditTagMain:
                        Model.MainState = EditState.Normal;
                        break;
                    case Operation.EditTagChildTags:
                        Model.ChildTagsState = EditState.Normal;
                        break;
                    case Operation.EditTagChildEntries:
                        Model.ChildEntriesState = EditState.Normal;
                        break;
                }
                await Refresh();
            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "撤销审核请求失败");
        }
    }
    public async void LocationChanged(object sender, LocationChangedEventArgs args)
    {
        if (Id != Model.Id.ToString())
        {
            if (_dataCatcheService.VideoIndexPageCatche.Check(Id) == false)
            {
                isLoading = true;
                StateHasChanged();
                await Refresh();
                isLoading = false;
                StateHasChanged();
            }
            else
            {
                await Refresh();
                StateHasChanged();
            }

        }
    }

    #region 释放实例

    public void Dispose()
    {

        NavigationManager.LocationChanged -= LocationChanged;

        GC.SuppressFinalize(this);
    }
    #endregion
}

